/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.Linq;
using TeachMe.Application.Data;

namespace TeachMe.Application
{
    using TeachMe.Application.ComboItems;

    [Flags]
    public enum EditedDictionaryValueType
    {
        Enabled = 1,
        Direction,
        Priority,
        Name,
        Description
    }

    public class EditedDictionaryDescriptor : IEditedItem
    {
        private Dictionary _dictionary;
        private EditedItemState _state;
        private EditedDictionaryValueType _modifiedValues;
        private EntryTextDirection _direction;
        private Priority _priority;
        private bool _enabled;

        public static EditedDictionaryDescriptor Create(Dictionary dictionary)
        {
            EditedDictionaryDescriptor result = new EditedDictionaryDescriptor();

            result._dictionary = dictionary;
            result._direction = dictionary.UserData.EntryTextDirection;
            result._priority = dictionary.UserData.Priority;
            result._enabled = dictionary.UserData.Enabled;
            result._state = EditedItemState.Normal;

            return result;
        }

        public static EditedDictionaryDescriptor CreateNew(Dictionary dictionary)
        {
            EditedDictionaryDescriptor result = new EditedDictionaryDescriptor();

            result._dictionary = dictionary;
            result._direction = dictionary.UserData.EntryTextDirection;
            result._priority = dictionary.UserData.Priority;
            result._enabled = dictionary.UserData.Enabled;
            result._state = EditedItemState.New;

            return result;
        }

        public EditedItemState State
        {
            get { return _state; }
            set { _state = value; }
        }

        public bool IsModified()
        {
            return _modifiedValues != 0;
        }

        public bool IsModified(EditedDictionaryValueType valueType)
        {
            return (_modifiedValues & valueType) == valueType;
        }

        public bool Enabled
        {
            get { return _enabled; }
            set
            {
                if (value != _enabled)
                {
                    _modifiedValues |= EditedDictionaryValueType.Enabled;
                }
                _enabled = value;
            }
        }

        public string Name
        {
            get
            {
                return _dictionary.Entries.Name;
            }
        }

        public string Description
        {
            get
            {
                return _dictionary.Entries.Description;
            }
        }

        public EntryTextDirection Direction
        {
            get
            {
                return this._direction;
            }
            set
            {
                if (this._direction != value)
                {
                    _modifiedValues |= EditedDictionaryValueType.Direction;
                }
                _direction = value;
            }
        }

        public Priority Priority
        {
            get
            {
                return _priority;
            }
            set
            {
                if (_priority != value)
                {
                    _modifiedValues |= EditedDictionaryValueType.Priority;
                }
                _priority = value;
            }
        }

        public Dictionary Dictionary
        {
            get { return _dictionary; }
        }

        public int Count
        {
            get { return _dictionary.Entries.Count; }
        }

        public PriorityComboBoxItem GridPriority
        {
            get
            {
                return ComboBoxItemsStore.Priorities.First(x => x.Item == _priority);
            }
            set
            {
                if (value.Item != _priority)
                {
                    _priority = value.Item;
                    _modifiedValues |= EditedDictionaryValueType.Priority;
                }
            }
        }

        public DirectionComboBoxItem GridDirection
        {
            get
            {
                return ComboBoxItemsStore.Directions.First(x => x.Item == this._direction);
            }
            set
            {
                if (value.Item != this._direction)
                {
                    this._direction = value.Item;
                    _modifiedValues |= EditedDictionaryValueType.Direction;
                }
            }
        }

        public string From
        {
            get
            {
                Languages.Item item = Singleton<Languages>.Instance.GetByCode(_dictionary.Entries.From);
                return item != null ? item.Localized : _dictionary.Entries.From;
            }
        }

        public string To
        {
            get
            {
                Languages.Item item = Singleton<Languages>.Instance.GetByCode(_dictionary.Entries.To);
                return item != null ? item.Localized : _dictionary.Entries.To;
            }
        }
    }
}
